import 'dart:async';

import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/apis/user.dart';
import 'package:qgchat/http/models/allgiftModel.dart';
import 'package:qgchat/http/models/anchorModel.dart';

class AnchorBloc implements BlocBase {

  void dispose(){
    _aInfo.close();
    _gift.close();
    _follow.close();
  }

  // ##########  VARIABLE  ##############

  // 主播信息
  AnchorModel anchorInfo;

  // ##########  STREAMS  ##############

  // 主播详情
  StreamController _aInfo = StreamController<AnchorModel>.broadcast();
  Sink get _aInfoSink => _aInfo.sink;
  Stream get aInfoStream => _aInfo.stream;

  // 主播礼物柜
  StreamController _gift = StreamController<AllgiftModel>.broadcast();
  Sink get _giftSink => _gift.sink;
  Stream get giftStream => _gift.stream;

  // 主播关注
  StreamController _follow = StreamController<int>.broadcast();
  Sink get __followSink => _follow.sink;
  Stream get followStream => _follow.stream;

  // ##########  HANDLING  ##############

  // 获取主播详情
  void bigVInfo(memberToken) async {
    AnchorModel result = await UserApis.bigVInfo(memberToken);
    anchorInfo = result;
    _aInfoSink.add(result);
  }

  // 获取礼物柜
  void allgift(memberToken) async {
    AllgiftModel result = await UserApis.allgift(memberToken);
    _giftSink.add(result);
  }

  // 关注
  void follow() async {
    Map isFollow = await UserApis.follow(anchorInfo.id);
    if (isFollow != null && isFollow['status'] == 1) {
      showToast('关注成功');
      __followSink.add(1);
    }
  }

  // 取消关注
  void cancelfollow() async {
    Map isCancelfollow = await UserApis.cancelfollow(anchorInfo.id);
    if (isCancelfollow != null && isCancelfollow['status'] == 1) {
      showToast('取消关注');
      __followSink.add(0);
    }
  }
}